﻿// 设置权限
var setAuthorize;
// 获取表单数据
var getFormData;
// 设置表单数据
var setFormData;
// 验证数据是否填写完整
var validForm;
var validBusinessData;
// 保存数据
var save;
var saveDraft;
//不可编辑
var setFormDisabled;

var isUpdate = false;

var bootstrap = function ($, learun) {
    "use strict";

    var userinfo;

    // 设置权限
    setAuthorize = function (data, isLook) {
        userinfo = learun.clientdata.get(['userinfo']);
        $("#createUsername").val(userinfo.user.realName);
        //报销人
        $("#userid").lrformselect({
            layerUrl: config.webportal_server + 'pages/system/user/selectOnlyForm.html?companyId=' + userinfo.user.companyId + '&access_token=' + config.getToken().access_token,
            layerUrlW: 400,
            layerUrlH: 300,
            dataUrl: config.base_server() + 'users/getNames?access_token=' + config.getToken().access_token,
        }).lrformselectSet(userinfo.user.id);
        //随行人员
        $("#followUserids").lrformselect({
            layerUrl: config.webportal_server + 'pages/system/user/selectForm.html?access_token=' + config.getToken().access_token,
            layerUrlW: 1024,
            layerUrlH: 768,
            dataUrl: config.base_server() + 'users/getNames?access_token=' + config.getToken().access_token,
        });

        $('#gridTable').css("width", $('.lr-form-wrap').width() - 60);

        //禁用输入框自动填充
        $("input[type='text']").each(function (index) {
            this.autocomplete = "off";
        });
    };
    //设置表单不可编辑
    setFormDisabled = function (wfType, upload) {
        if (!!wfType && wfType == 100) {
            $('#form').find('input,textarea,select').attr('disabled', 'disabled');
            if(upload != '1') {
                $('#lrUploader_uploadBtn_files').hide();
            }
            $("#userid").unbind();
            $("#destination").unbind();
            $("#followUserids").unbind();
        }
    };
    // 获取表单数据
    getFormData = function () {
        var formData = $('#form').lrGetFormData();
        formData.vehicle = "";
        $("#vehicle").find('input[type="checkbox"]').each(function (r) {
            if ($(this).prop("checked")) {
                if (formData.vehicle == "") {
                    formData.vehicle += $(this).val();
                } else {
                    formData.vehicle += ("," + $(this).val());
                }

            }
        });

        var postData = formData;
        postData.id = parent.processId;
        return postData;
    };
    // 设置表单数据
    setFormData = function (processId, wfType, checkPermission, upload) {
        if (processId) {
            $('#files').lrUploader({isView: false});
            $.lrSetForm(config.base_server() + 'businesstrip1s/findByProcessId?processId=' + processId, function (data) {
                console.log(data);
                if (data != null) {
                    isUpdate = true;
                    $('#form').lrSetFormData(data);
                    $("#processCode").html(data.processCode);
                    var destinationStr = data.destinationStr.split('/');
                    $("#destination").citypicker({
                        province: destinationStr[0],
                        city: !!destinationStr[1] ? destinationStr[1] : "",
                        district: !!destinationStr[2] ? destinationStr[2] : ""
                    });
                    $("#vehicle").find('input[type="checkbox"]').each(function (r) {
                        if (data.vehicle.indexOf($(this).val()) >= 0) {
                            $(this).prop("checked", true);
                        }
                    });
                } else {
                    $("#destination").citypicker();
                }
                setPbtn(wfType, checkPermission);
            });
        } else {
            setPbtn(wfType, checkPermission);
        }
        setFormDisabled(wfType, upload);
    };
    // 验证数据是否填写完整
    validForm = function (code) {
        if (!$('#form').lrValidform()) {
            return false;
        }
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        if (endTime < startTime) {
            learun.alert.error("请正确填写日期区间");
            return false;
        }
        if (code == 'create') {
            if ($('#files').find('.lrUploader-input').text() == '') {
                learun.alert.error("请上传【附件】");
                return false;
            }
        }

        return true;
    };
    validBusinessData = function () {
        return true;
    }
    // 保存调用函数
    save = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();
        formData.vehicle = "";
        $("#vehicle").find('input[type="checkbox"]').each(function (r) {
            if ($(this).prop("checked")) {
                if (formData.vehicle == "") {
                    formData.vehicle += $(this).val();
                } else {
                    formData.vehicle += ("," + $(this).val());
                }

            }
        });


        var postData = formData;
        postData.id = parent.processId;

        $.lrSaveForm(config.base_server() + 'businesstrip1s/saveOrUpdate?keyValue=' + keyValue + '&force=' + force + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };

    // 保存调用函数
    saveDraft = function (processId, force, callBack, i) {
        var keyValue = "";

        if (isUpdate) {
            keyValue = processId;
        }

        var formData = $('#form').lrGetFormData();
        formData.vehicle = "";
        $("#vehicle").find('input[type="checkbox"]').each(function (r) {
            if ($(this).prop("checked")) {
                if (formData.vehicle == "") {
                    formData.vehicle += $(this).val();
                } else {
                    formData.vehicle += ("," + $(this).val());
                }

            }
        });


        var postData = formData;
        postData.id = parent.processId;

        $.lrSaveForm(config.base_server() + 'businesstrip1s/saveOrUpdate?keyValue=' + keyValue + '&force=' + force + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack(res, i);
            }
        });
    };


    // 设置父级窗口的按钮
    function setPbtn(wfType, checkPermission) {
        if (!!wfType && wfType == 100 && checkPermission == true) { //审核
            // $('#lr-layout-panel-right', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#flow-projectinfo', window.parent.document).hide();
        } else if (!!wfType && wfType == 100 && checkPermission == false) { //查看
            $('#lr-layout-panel-right', window.parent.document).empty();
        } else {
            $('#release', window.parent.document).text('提交申请');
            // $('.lr-form-item-title', window.parent.document).text('备注');
            //$('#savedraft', window.parent.document).hide();
            $('#verifyBill', window.parent.document).hide();
            $('#flow-projectinfo', window.parent.document).hide();
            $('#flow-force', window.parent.document).hide();
            $('#sign', window.parent.document).hide();
            $('#flow-title input', window.parent.document).val("出差申请");
            $('#flow-title input', window.parent.document).attr("readonly", "readonly");
            //$('#print', window.parent.document).hide();
        }
    }

}
